<template>
    <el-card style="width: 500px;">
        <el-form :model="formData" label-width="80px">
            <el-upload
                    class="avatar-uploader"
                    action="http://localhost:9090/files/upload/"
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess">
                <el-avatar v-if="formData.avatar" :src="formData.avatar"  class="avatar"></el-avatar>
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <el-form-item label="ID">
                <el-input v-model="formData.id" autocomplete="off" disabled></el-input>
            </el-form-item>
            <el-form-item label="姓名">
                <el-input v-model="formData.username" autocomplete="off" disabled></el-input>
            </el-form-item>
            <el-form-item label="昵称">
                <el-input v-model="formData.nickname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
                <el-input v-model="formData.email" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="电话">
                <el-input v-model="formData.phone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="地址">
                <el-input type="textarea" v-model="formData.address" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleEdit">确 定</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
    export default {
        name: "Person",
        data() {
            return {
                formData: {},
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
            }
        },
        created() {
            this.getUser().then(res => {
                this.formData = res;
            });
        },
        methods: {
            async getUser() {
                return (await this.request.get("/users/username/" + this.user.username)).data;
            },
            handleEdit() {
                this.request.post("/users", this.formData).then((res) => {
                    if (res.flag) {
                        //触发父级的更新user方法
                        this.$emit("refreshUser");
                        //更新浏览器存储的用户消息
                        this.getUser().then(res => {
                            res.token = JSON.parse(localStorage.getItem("user")).token;
                            localStorage.setItem("user", JSON.stringify(res));
                        });
                        this.$message.success("保存成功");
                    } else {
                        this.$message.error("保存失败");
                    }
                })
            },
            handleAvatarSuccess(res, file) {
                this.formData.avatar = res;
            },
            /*beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }*/
        }

    }
</script>

<style scoped>
    .avatar-uploader {
        text-align: center;
        margin-bottom: 10px;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 148px;
        height: 148px;
        line-height: 148px;
        text-align: center;
    }

    .avatar {
        width: 148px;
        height: 148px;
        display: block;
    }
</style>